package com.dandelion.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dandelion.business.entity.SysRole;
import com.dandelion.business.entity.vo.role.req.RoleReqVO;
import com.dandelion.business.entity.vo.user.CurrentUser;
import com.dandelion.business.mapper.SysRoleMapper;
import com.dandelion.business.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dandelion.framework.common.R;
import com.dandelion.framework.enums.CodeEnum;
import com.dandelion.framework.enums.DelEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2021-12-26
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    /**
     * 角色列表
     *
     * @return {@link R}
     */
    @Override
    public R<List<SysRole>> roleList() {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getDelFlag, DelEnum.NOT_DEL.val);
        return R.ok().data(list(wrapper));
    }

    /**
     * 添加
     *
     * @param currentUser  铜
     * @param req 要求事情
     * @return {@link R}
     */
    @Override
    public R<Boolean> add(CurrentUser currentUser, RoleReqVO req) {
        try {
            SysRole role = new SysRole();
            BeanUtil.copyProperties(req, role);
            role.setCreateBy(currentUser.getUserId());
            role.setCreateTime(LocalDateTime.now());
            role.setUpdateBy(currentUser.getUserId());
            role.setUpdateTime(LocalDateTime.now());
            boolean save = save(role);
            if (save) {
                return R.ok().data(true);
            }
        } catch (Exception e) {
            log.warn("保存角色异常:{}", e.getMessage());
        }
        return R.fail(CodeEnum.ADD_ROLE_ERROR).data(false);
    }

    /**
     * 查询角色
     *
     * @param id id
     * @return {@link R}
     */
    @Override
    public R<SysRole> queryRole(Long id) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getId, id);
        wrapper.eq(SysRole::getDelFlag, DelEnum.NOT_DEL.val);
        return R.ok().data(getOne(wrapper));
    }

    /**
     * 编辑菜单
     *
     * @param currentUser  铜
     * @param req 要求事情
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> editMenu(CurrentUser currentUser, RoleReqVO req) {
        Long id = req.getId();
        SysRole role = getById(id);

        String name = req.getName();
        if (StrUtil.isNotBlank(name)) {
            role.setName(name);
        }
        String roleKey = req.getRoleKey();
        if (StrUtil.isNotBlank(roleKey)) {
            role.setRoleKey(roleKey);
        }
        Integer state = req.getState();
        if (state != null) {
            role.setState(state);
        }

        String remark = req.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            role.setRemark(remark);
        }
        role.setUpdateBy(currentUser.getUserId());
        role.setUpdateTime(LocalDateTime.now());
        boolean update = false;
        try {
            update = updateById(role);
            if (update) {
                return R.ok().data(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail(CodeEnum.UPDATE_ROLE_ERROR).data(update);
    }


}
